local huiqi = fk.CreateSkill{
  name = "hanqing__huiqi",
  tags = { Skill.Limited },
  dynamic_desc = function (self, player, lang)
    if #player:getTableMark(self.name) > 0 then
      local str = {}
      for i = 1, 3 do
        if table.contains(player:getTableMark(self.name), i) then
          table.insert(str, Fk:translate("hanqing__huiqi_stab__slash"))
        else
          table.insert(str, Fk:translate("hanqing__huiqi_slash"))
        end
      end
      return "hanqing__huiqi_inner:"..str[1]..":"..str[2]..":"..str[3]
    end
  end,
}

Fk:loadTranslationTable{
  ["hanqing__huiqi"] = "慧起",
  [":hanqing__huiqi"] = "限定技，出牌阶段，你可以视为使用【杀】、【杀】、【杀】；出牌阶段开始时，你可以将上述一张【杀】改为"..
  "“无距离限制的刺【杀】”。",

  [":hanqing__huiqi_inner"] = "限定技，出牌阶段，你可以视为使用{1}、{2}、{3}；出牌阶段开始时，你可以将上述一张【杀】改为"..
  "“无距离限制的刺【杀】”。",

  ["#hanqing__huiqi-slash"] = "慧起：你可以视为使用【杀】！",
  ["#hanqing__huiqi-stab__slash"] = "慧起：你可以视为使用无距离限制的【刺杀】！",
  ["#hanqing__huiqi-choice"] = "慧起：你可以将“【杀】”改为“无距离限制的刺【杀】”",
  ["hanqing__huiqi_slash"] = "【杀】",
  ["hanqing__huiqi_stab__slash"] = "无距离限制的刺【杀】",
}

huiqi:addEffect("viewas", {
  anim_type = "offensive",
  mute_card = false,
  prompt = function (self, player)
    if not table.contains(player:getTableMark(huiqi.name), 1) then
      return "#hanqing__huiqi-slash"
    else
      return "#hanqing__huiqi-stab__slash"
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 then return end
    local card = Fk:cloneCard("slash")
    if table.contains(player:getTableMark(huiqi.name), 1) then
      card = Fk:cloneCard("stab__slash")
    end
    card.skillName = huiqi.name
    return card
  end,
  before_use = function (self, player, use)
    use.extraUse = true
  end,
  after_use = function (self, player, use)
    local room = player.room
    for i = 2, 3 do
      if not player.dead then
        local name = "slash"
        if table.contains(player:getTableMark(huiqi.name), i) then
          name = "stab__slash"
        end
        if not room:askToUseVirtualCard(player, {
          name = name,
          skill_name = huiqi.name,
          prompt = "#hanqing__huiqi-"..name,
          cancelable = true,
          extra_data = {
            bypass_times = true,
            extraUse = true,
          },
        }) then
          return
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(huiqi.name, Player.HistoryGame) == 0
  end,
})

huiqi:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and table.contains(card.skillNames, huiqi.name) and card.name == "stab__slash"
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    return card and table.contains(card.skillNames, huiqi.name)
  end,
})

huiqi:addEffect(fk.EventPhaseStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(huiqi.name) and player.phase == Player.Play and
      #player:getTableMark(huiqi.name) < 3
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local all_choices = {}
    for i = 1, 3 do
      if table.contains(player:getTableMark(huiqi.name), i) then
        table.insert(all_choices, "hanqing__huiqi_stab__slash")
      else
        table.insert(all_choices, "hanqing__huiqi_slash:::"..i)
      end
    end
    table.insert(all_choices, "Cancel")
    local choices = table.filter(all_choices, function (choice)
      return choice ~= "hanqing__huiqi_stab__slash"
    end)
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = huiqi.name,
      prompt = "#hanqing__huiqi-choice",
      all_choices = all_choices,
    })
    if choice ~= "Cancel" then
      local n = tonumber(string.split(choice, ":")[4])
      event:setCostData(self, {choice = n})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(huiqi.name)
    room:notifySkillInvoked(player, self.name, "offensive")
    room:addTableMark(player, huiqi.name, event:getCostData(self).choice)
  end,
})

huiqi:addAI(nil, "vs_skill")

return huiqi
